/*
 * CS_SOCIAL_BOAST.cpp
 *
 *  Created on: 2013. 5. 27.
 *      Author: the2
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../Proxy.h"
#include "../../Enum.h"
#include "../../../../protocol/iFriends.h"
#include "../../NResource/Static.h"

#include "../../NLogic/Function.h"

#include "../../NProcess/Static.h"
#include <Toolkit/Str.h>

#include <sstream>

namespace ProjectZ { namespace NCommand {

void SendPushBOAST(User * pUser, int receiveUserSeq, int messageType)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT company, sale_cd, uuid FROM info_user WHERE user_seq = %d", receiveUserSeq);
	if (false == ::Database::GETSESSION()->SyncExecute(static_cast<int>(DatabaseType::GAME_SLAVE), clsAuto))
		return;

	if( clsAuto->GetErrorIdx() != 0 )
	{
		DEBUGLOG("%s clsAuto->GetErrorIdx() != 0", __FUNCTION__ );
		return;
	}

	if (0 == clsAuto->ResultRowCount())
	{
		DEBUGLOG("%s 0 == clsAuto->ResultRowCount()", __FUNCTION__ );
		return;
	}

	int company = 0;
	int sale_cd = 0;
	::Toolkit::Str<81> uuid;

	while( clsAuto->Next() )
	{
		clsAuto->GetRecord() >> company >> sale_cd >> uuid;
	}

	DEBUGLOG("CS_SOCIAL_BOAST msg.type : %d", messageType);

	if (messageType == 0)
	{
		//NProcess::PushSender::Param clsParam;
		NProcess::PushSender::Param * clsParam = NProcess::Static::Instance().GetParamPool()->SAFE_NEW();
		if (company == 5)
			clsParam->_eType = NProcess::PushSender::Param::Type::ANDROID;
		else
			clsParam->_eType = NProcess::PushSender::Param::Type::IOS;


//		clsParam->_sender = "어머! 천부적인 싸움꾼!";
//
//		std::stringstream stream;
//		stream << "[";
//		stream << pUser->GetUserInfo().nickname.c_str();
//		stream << "]님의 주간랭킹이 상승했네요. 주인님도 분발하세요!!!";
//
//		clsParam->_message = stream.str();
//		clsParam->_uuid = uuid.c_str();

		NResource::MessageResource * pMessageResource = NResource::Static::InstancePtr()->GetMessageResource();
		if (NULL == pMessageResource)
			return;

		std::string sender;
		std::string message;

		if (false == pMessageResource->SetMessageFromType(company, sale_cd, NResource::MessageData::message_type::boast, sender, message))
			return;

		Toolkit::Str<400> sendMessage;
		sendMessage.Format(message.c_str(), pUser->GetUserInfo().nickname.c_str());

		clsParam->_sender	= sender.c_str();
		clsParam->_message	= sendMessage.c_str();
		clsParam->_uuid		= uuid.c_str();

		NProcess::Static::Instance().GetPushSender().Push(clsParam);
	}
}

void Command::CS_SOCIAL_BOAST(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	BoastSyn msg;

	*pPacket >> msg.gid >> msg.type >> msg.param >> msg.subparam;

	if( msg.gid <= 0 )
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->MsgCat("%2d%d", ACK_UNKNOWN_ERROR, 0);
		pAck->MakePacket(CMD_SC_SOCIAL_BOAST);
		pUser->Send(pAck);
		return;
	}

	NResource::BalanceResource * balanceResource = NResource::Static::InstancePtr()->GetBalance();
	msg.heart_count_max = balanceResource->GetValue(pUser, EM_FRIEND_POINT_MAX);

	NResource::SocialRewardResource * socialReward = NResource::Static::InstancePtr()->GetSocialReward();
	NResource::SocialRewardData::Flyweight * rewardData = socialReward->GetFly(pUser, msg.type + NResource::SocialRewardData::BoastPvPRankUp);

	msg.send_heart_count = rewardData->_SEND_REWARD * rewardData->_IS_EVENT;
	msg.recv_heart_count = rewardData->_RECV_REWARD * rewardData->_IS_EVENT;
	msg.msg_type = rewardData->_MSG_TYPE;

	::ProjectZ::NProxy::SendFriendsServer(pUser->GetUserSeq(), msg);


//	DEBUGLOG("CS_SOCIAL_BOAST msg.type : %d", msg.type);
//	if (msg.type == 0)
//	{
//		std::stringstream stream;
//		stream << "주인님!! [";
//		stream << pUser->GetUserInfo().nickname.c_str();
//		stream << "]님의 주간랭킹이 상승했어요.";
//
//		NLogic::NFunction::SendPush(msg.gid, stream.str());
//	}

	SendPushBOAST(pUser, msg.gid, msg.type);
}

} /*NCommand*/ } /*ProjectZ*/
